<template>
  <div class="table">
    <div style="margin: 10px 0;padding: 0 5px;display: flex">
      <el-popconfirm style="padding-left: 5px;padding-right: 5px" title="确认批量删除吗?" @confirm="delBatch">
        <template #reference>
          <el-button type="danger">批量删除</el-button>
        </template>
      </el-popconfirm>
      <el-button plain icon="el-icon-left" style="margin-left: auto;margin-right: 40px" @click="()=>$router.back()">返回
      </el-button>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="40%">
      <el-form label-width="90px" width="30%">
        <el-form-item label="评论者姓名" prop="username">
          <el-input v-model="editForm.username" disabled ></el-input>
        </el-form-item>
        <el-form-item label="评论时间" prop="birth">
          <el-date-picker type="date" value-format="yyyy-MM-dd" v-model="editForm.createTime"></el-date-picker>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="editForm.content"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
      </template>
    </el-dialog>

    <!--显示乐谱-->
    <el-table :data="tableData" stripe style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="序号" v-if="false"></el-table-column>
      <el-table-column prop="username" label="评论者名"></el-table-column>
      <el-table-column prop="createTime" label="评论时间">
        <template slot-scope="scope">
          {{ attachBirth(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="content" label="内容" align="center"></el-table-column>

      <!--按钮 -->
      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope.row)" icon="el-icon-edit"></el-button>
          <el-popconfirm title="这是一段内容确定删除吗？" @confirm="handleDelete(scope.row.id)">
            <el-button slot="reference" type="danger" style="margin-left: 6px" icon="el-icon-delete"></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div style="padding: 10px 0;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>

import {deleteComment, deleteComments, getAllComment, updateComment,} from "../api/index";
import {mixin} from "@/mixins";
import showXml from '../util/showxml'


export default {
  mixins: [mixin],
  name: "Comment",
  data() {
    return {
      staffId: '',//乐谱id
      //查询
      tableData: [],
      total: 0,//分页，默认总数为0
      pageNum: 1,//分页，默认第一页
      pageSize: 5,//分页，默认一页有两条
      editForm: {
        content: '',
        username: '',
        createTime: '',
      },
      editVisible: false,//默认不展示
      multipleSelection: [],//批量删除选中集合
    }
  },

  created() {
    this.staffId = this.$route.query.id;
    this.getData();
  },
  //更新图片
  methods: {
    uploadUrl(id) {
      return `http://localhost:8888/consumer/upload?id=${this.$route.query.id}`
    },
    //图片更新成功之后
    filesUploadSuccess() {
      this.getData()//刷新表格
    },
    //查询评论
    getData() {
      this.tableData = [];//清空tableData里的数据
      getAllComment(this.staffId).then(res => {
        console.log({res})
        this.tableData.push(...res.data);
      })
    },

    // 处理搜索按钮点击
    handleQuery() {
      this.pageNum = 1;
      this.getData();
    },
    //分页
    handleSizeChange(pageSize) {
      console.log(`每页 ${pageSize} 条`);
      this.pageSize = pageSize
      this.getData()//调用分页方法
    },
    handleCurrentChange(pageNum) {
      console.log(`当前页: ${pageNum}`);
      this.pageNum = pageNum
      this.getData()//调用分页方法
    },
    //操作----编辑
    handleEdit(row) {
      Object.assign(this.editForm, row) //把数据显示在表单上
      this.editVisible = true//打开弹窗
    },
    //编辑确定按钮
    saveEdit() {
      updateComment(this.editForm).then(res => {
        if (res) {
          this.$message.success("修改成功")
          this.editVisible = false//关闭弹窗
          this.getData()//刷新数据
        } else {
          this.$message.error("修改失败")
          this.editVisible = false//关闭弹窗
        }
      })
    },
    //删除
    handleDelete(id) {
      deleteComment(id).then(res => {
        if (res) {
          this.$message.success("删除成功")
          this.getData()
        } else {
          this.$message.success("删除失败")
        }
      })
    },
    //批量删除
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    //批量删除按钮
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)//把对象数组变成集合[{},{},{}] ----> [1,2,3]
      deleteComments(ids).then(res => {
        if (res) {
          this.$message.success("批量删除成功")
          this.getData()
        } else {
          this.$message.success("批量删除失败")
        }
      })
    },
    //查看乐谱
    previewXml(fileUrl){
      //这里传入不同的url，展示不同的文件
      showXml(fileUrl)
    }
  }
}
</script>

<style scoped>

</style>
